﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class TemplateGroupQuestionTreeView
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string RootGroupQuestionId = (string)WebEnvironments.Request(SiteId, "RId");
                if (string.IsNullOrEmpty(RootGroupQuestionId)) RootGroupQuestionId = "";

                string ExamTemplateId = (string)WebEnvironments.Request(SiteId, "EId");
                if (string.IsNullOrEmpty(ExamTemplateId)) ExamTemplateId = "";

                string AccountType = (string)WebEnvironments.Request(SiteId, "AccountType");
                if (string.IsNullOrEmpty(AccountType)) AccountType = "";

                string SelectLevelText =
                    "<select id=drpSelectLevelQuestion class=drplist1>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";

                string Html =

                    #region js
 "<script language=javascript>\r\n" +
                    "     function BackFromAddGroupQuestion()\r\n" +
                    "     {\r\n" +
                    "         document.getElementById('divFormAddGroupQuestion').innerHTML='';\r\n" +
                    "         document.getElementById('divFormAddGroupQuestion').style.display='none';\r\n" +
                    "         document.getElementById('divMainForm').style.display='block';\r\n" +
                    "     }\r\n" +

                    "     function BackFromUpdateGroupQuestion()\r\n" +
                    "     {\r\n" +
                    "         document.getElementById('divFormUpdateGroupQuestion').innerHTML='';\r\n" +
                    "         document.getElementById('divFormUpdateGroupQuestion').style.display='none';\r\n" +
                    "         document.getElementById('divMainForm').style.display='block';\r\n" +
                    "     }\r\n" +

                    "     function AddTreeNode(id,title)\r\n" +
                    "     {\r\n" +
                    "           var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "           var childNode=null;\r\n" +
                    "           if(SelectedNode!=null)\r\n" +
                    "           {\r\n" +
                    "               childNode = SelectedNode.addChild({\r\n" +
                    "                       isFolder: true\r\n" +
                    "               });\r\n" +
                    "               childNode.data.href=id;\r\n" +
                    "               childNode.setTitle(title);\r\n" +
                    "           }\r\n" +
                    "     }\r\n" +

                    "     function SaveTreeNode(id,title)\r\n" +
                    "     {\r\n" +
                    "          var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "          SelectedNode.setTitle(title);\r\n" +
                    "     }\r\n" +


                    "     function ClientAddGroupQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "         if(SelectedNode==null)\r\n" +
                    "         {\r\n" +
                    "            alert('Chưa xác định vào nhóm cần thêm!');\r\n" +
                    "            return;\r\n" +
                    "         }\r\n" +
                    "         ParentGroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         RootGroupQuestionId='" + RootGroupQuestionId + "';\r\n" +
                    "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetAddGroupQuestion(SiteId,RootGroupQuestionId, ParentGroupQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         document.getElementById('divFormAddGroupQuestion').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "         document.getElementById('divFormAddGroupQuestion').style.display='block';\r\n" +
                    "         document.getElementById('divMainForm').style.display='none';\r\n" +
                    "     }\r\n" +

                    "     function ClientAddQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         ParentGroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetAddQuestion(SiteId,RId, ParentGroupQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "     }\r\n" +


                    "     function ClientImport(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         ParentGroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         RootGroupQuestionId='" + RootGroupQuestionId + "';\r\n" +
                    "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetImportGroupQuestion(SiteId,RootGroupQuestionId, ParentGroupQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         document.getElementById('divFormImportGroupQuestion').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "         document.getElementById('divFormImportGroupQuestion').style.display='block';\r\n" +
                    "         document.getElementById('divMainForm').style.display='none';\r\n" +
                    "     }\r\n" +

                    "     function ClientUpdateGroupQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "         if(SelectedNode==null)\r\n" +
                    "         {\r\n" +
                    "            alert('Chưa xác định vào nhóm cần sửa!');\r\n" +
                    "            return;\r\n" +
                    "         }\r\n" +
                    "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetUpdateGroupQuestion(SiteId,GroupQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         document.getElementById('divFormUpdateGroupQuestion').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "         document.getElementById('divFormUpdateGroupQuestion').style.display='block';\r\n" +
                    "         document.getElementById('divMainForm').style.display='none';\r\n" +
                    "     }\r\n" +



                    "     function ClientDeleteGroupQuestion(RId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         if(GroupQuestionId=='')\r\n" +
                    "         {\r\n" +
                    "               alert('Chưa xác định nhóm cần xóa');\r\n" +
                    "               return;\r\n" +
                    "         }\r\n" +
                    "         if(GroupQuestionId==RId)\r\n" +
                    "         {\r\n" +
                    "               alert('Không xóa được nhóm gốc');\r\n" +
                    "               return;\r\n" +
                    "         }\r\n" +
                    "         if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideDeleteGroupQuestion(SiteId,GroupQuestionId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    "         SelectedNode.remove();\r\n" +
                    "         tree.activateKey(RId);\r\n" +
                    "         ClientLoadQuestion(RId);\r\n" +
                    "     }\r\n" +

                    "   function DeleteQuestion(GroupQuestionId,QuestionId)\r\n" +
                    "   {\r\n" +
                    "       if(confirm('Bạn đã chắc chắn xóa câu hỏi này chưa?')==false)return;\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       Keyword=document.getElementById('txtFilterKeyword').value;\r\n" +
                    "      ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                    "         SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    "         SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideDeleteQuestion(SiteId,GroupQuestionId,QuestionId,Keyword,ExamTemplateId,SelectLevel).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "   }\r\n" +


                    "   function ViewQuestion(GroupQuestionId,QuestionId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideViewQuestion(SiteId,GroupQuestionId,QuestionId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function UpdateQuestion(GroupQuestionId,QuestionId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideUpdateQuestion(SiteId,GroupQuestionId,QuestionId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +


                    "   function SearchExamQuestion()\r\n" +
                    "   {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                    "         if(GroupQuestionId=='')\r\n" +
                    "         {\r\n" +
                    "               alert('Chưa xác định nhóm cần đưa câu hỏi vào');\r\n" +
                    "               return;\r\n" +
                    "         }\r\n" +
                    "       AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetSearchForm(SiteId,'" + RootGroupQuestionId + "',GroupQuestionId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divSearchExamQuestion').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       document.getElementById('divSearchExamQuestion').style.display='block';\r\n" +
                    "       document.getElementById('divMainForm').style.display='none';\r\n" +
                    "   }\r\n" +


                    "   function ClientBackFromImport()\r\n" +
                    "   {\r\n" +
                    "       document.getElementById('divFormImportGroupQuestion').innerHTML='';\r\n" +
                    "       document.getElementById('divFormImportGroupQuestion').style.display='none';\r\n" +
                    "       document.getElementById('divMainForm').style.display='block';\r\n" +
                    "   }\r\n" +

                    ///////////////// Import file

                     "function fileSelected() {\r\n" +
                        "    var file = document.getElementById('fileToUpload').files[0];\r\n" +
                        "    if (file) {\r\n" +
                        "        var fileSize = 0;\r\n" +
                        "        if (file.size > 1024 * 1024)\r\n" +
                        "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                        "        else\r\n" +
                        "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                        "        document.getElementById('fileName').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                        "        document.getElementById('fileSize').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                        "        document.getElementById('fileType').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                        "    }\r\n" +
                        "}\r\n" +

                        "function ClientExecuteTemplateGroupQuestionTreeView() {\r\n" +
                        "    if(confirm('Bạn đã chắc chắn nhập dữ liệu từ file chưa?')==false)return;\r\n" +
                        "    var fd = new FormData();\r\n" +
                        "    fd.append(\"fileToUpload\", document.getElementById('fileToUpload').files[0]);\r\n" +
                        "    var xhr = new XMLHttpRequest();\r\n" +
                        "    xhr.upload.addEventListener(\"progress\", uploadProgress, false);\r\n" +
                        "    xhr.addEventListener(\"load\", uploadComplete, false);\r\n" +
                        "    xhr.addEventListener(\"error\", uploadFailed, false);\r\n" +
                        "    xhr.addEventListener(\"abort\", uploadCanceled, false);\r\n" +
                        "    xhr.open(\"POST\", \"UploadHandler.ashx?SiteId=" + SiteId + "\");\r\n" +
                        "    xhr.send(fd);\r\n" +
                        " }\r\n" +

                        "function uploadProgress(evt) {\r\n" +
                        "    if (evt.lengthComputable) {\r\n" +
                        "    }\r\n" +
                        "    else {\r\n" +
                        "    }\r\n" +
                        "}\r\n" +

                        "function uploadComplete(evt) {\r\n" +
                        "    if(evt.target.responseText=='')\r\n" +
                        "    {\r\n" +
                        "       alert('Chưa chọn file để import!');\r\n" +
                        "       return;\r\n" +
                        "    }\r\n" +
                        "    /* This event is raised when the server send back a response */\r\n" +
                        "    document.getElementById('txtImportFile').value=evt.target.responseText;\r\n" +
                        "    //doan nay chay upload vao day...\r\n" +
                        "    ProcessImportFile()\r\n" +
                        "}\r\n" +

                        "function uploadFailed(evt) {\r\n" +
                        "    alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                        "}\r\n" +

                        "function uploadCanceled(evt) {\r\n" +
                        "    alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                        "}\r\n" +


                        "function ProcessImportFile()\r\n" +
                        "{\r\n" +
                        "    SiteId='" + SiteId + "';\r\n" +
                        "    ImportFile=document.getElementById('txtImportFile').value;\r\n" +
                        "    if(ImportFile=='')\r\n" +
                        "    {\r\n" +
                        "       alert('Chưa có file để nhập liệu!');\r\n" +
                        "       return;\r\n" +
                        "    }\r\n" +

                        "    AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideReadMaxLine(SiteId, ImportFile).value;\r\n" +
                        "    MaxLine=AjaxOut.RetNumber;\r\n" +
                        "    if(MaxLine<=1)\r\n" +
                        "    {\r\n" +
                        "       alert('Không có dữ liệu để import!');\r\n" +
                        "       return;\r\n" +
                        "    }\r\n" +
                        "    StartLine=1;\r\n" +
                        "    document.getElementById('divImportLog').innerHTML='';\r\n" +
                        "    document.getElementById('divImportLog').style.display='block';\r\n" +
                        "    setTimeout('ImportData()',30);\r\n" +
                        "}\r\n" +

                        "function ImportData()\r\n" +
                        "{\r\n" +
                        "    if(StartLine==MaxLine)\r\n" +
                        "    {\r\n" +
                        "       StartLine=1;\r\n" +
                        "       MaxLine=0;\r\n" +
                        "       ProcessBarText = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetProcessBar(SiteId, MaxLine, MaxLine).value;\r\n" +
                        "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                        "    }\r\n" +
                        "    else\r\n" +
                        "    {\r\n" +
                        "       ProcessBarText = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideGetProcessBar(SiteId, StartLine, MaxLine).value;\r\n" +
                        "       document.getElementById('divProcessing').innerHTML=ProcessBarText;\r\n" +
                        "       ImportFile=document.getElementById('txtImportFile').value;\r\n" +
                        "       GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                        "       if(GroupQuestionId=='')\r\n" +
                        "       {\r\n" +
                        "              alert('Chưa xác định nhóm cần đưa câu hỏi vào');\r\n" +
                        "              return;\r\n" +
                        "       }\r\n" +
                        "       SelectType=document.getElementById('drpSelectType').value;\r\n" +
                        "       Level=document.getElementById('drpSelectLevelTemplateGroupQuestionTreeView').value;\r\n" +
                        "       Overwrite=parseInt(document.getElementById('drpSelectOverwrite').value);\r\n" +
                        "       AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideProcessLine(SiteId, GroupQuestionId, SelectType, Level , ImportFile, StartLine, Overwrite).value;\r\n" +
                        "       ImportLog=document.getElementById('divImportLog').innerHTML;\r\n" +
                        "       if(AjaxOut.Error)\r\n" +
                        "       {\r\n" +
                        "           document.getElementById('divImportLog').innerHTML=AjaxOut.InfoMessage+'<br>'+ImportLog;\r\n" +
                        "       }\r\n" +
                        "       StartLine=StartLine+1;\r\n" +
                        "       setTimeout('ImportData()',30);\r\n" +
                        "    }\r\n" +
                        "}\r\n" +

                        " function ClientSelectExamQuestionTemp(QuestionId)\r\n" +
                        " {\r\n" +
                        "     ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                    //"    SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    //"    SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                        "     AjaxOut=Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideAddToTemplate(SiteId,ExamTemplateId,QuestionId).value;\r\n" +
                        "     if(AjaxOut.Error)\r\n" +
                        "     {\r\n" +
                        "         alert(AjaxOut.InfoMessage);\r\n" +
                        "         return;\r\n" +
                        "     }\r\n" +
                        "      document.getElementById('divQuestion'+QuestionId).innerHTML='';\r\n" +
                        " }\r\n" +

                    "</script>\r\n" +

                #endregion

                    GroupQuestionRenderBll.GetPlugInAddGroupQuestionJavascript(SiteId, true) +
                    GroupQuestionRenderBll.GetPlugInUpdateGroupQuestionJavascript(SiteId, true) +
                    SearchExamQuestion.GetPluginJavascript(SiteId) +

                    "<div id=divFormImportGroupQuestion></div>\r\n" +
                    "<div id=divFormAddGroupQuestion></div>\r\n" +
                    "<div id=divFormUpdateGroupQuestion></div>\r\n" +
                    "<div id=divSearchExamQuestion></div>\r\n" +
                    "<div class=CssFormData id=divMainForm>\r\n" +
                        DrawFunction(SiteId, RootGroupQuestionId, AccountType).HtmlContent +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=col1><div class=CssDivGroupQuestion>" +
                                DrawGroupQuestion(SiteId, RootGroupQuestionId,AccountType).HtmlContent + "</div></td>\r\n" +
                    "       <td class=col2>\r\n" +
                    "           <h3>Danh sách câu hỏi trong nhóm "+
                    "               <div class=\"filterquestion\">"+
                    "                       Tiêu đề câu hỏi:"+
                    "                       <input id=txtFilterKeyword style=\"margin-right:5px;\">" +                    
                    "                       Phân loại:" +
                                            SelectLevelText +
                    "                       <input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ClientRefresh(0);\">" +
                    "               </div>"+
                    "           </h3>\r\n" +
                    "           <div class=CssQuestionList id=\"divQuestionList\">" +
                                       ServerSideReadingQuestionInGroupQuestion(SiteId, RootGroupQuestionId, "", 0, ExamTemplateId,"0").HtmlContent + "\r\n" +
                    "           </div>\r\n" +
                    "       </td>\r\n" +
                    "           <input type=hidden id=txtExamTemplateId value=\"" + ExamTemplateId + "\">" +
                    "   </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddToTemplate(
            string SiteId,
            string ExamTemplateId,
            string QuestionId
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new QuestionBll().AddQuestionToExamTemplate(null, SiteId, ExamTemplateId, QuestionId, "");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawGroupQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = string.Empty;
                GroupQuestionCls[] OGroupQuestion = GroupQuestionBll.ReadingGroupQuestion(SiteId, RootGroupQuestionId);
                Html =
                              "<script language=javascript>\r\n" +
                              "     function ClientLoadQuestion(GroupQuestionId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         document.getElementById('txtSelectedGroupQuestionId').value = GroupQuestionId;\r\n" +
                              "         Keyword=document.getElementById('txtFilterKeyword').value;\r\n" +
                              "         CurrentPageIndex=parseInt(document.getElementById('txtCurrentPageIndex').value);\r\n" +
                              "         ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                              "         SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                              "         SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                              "         AjaxOut = Portal.Exam.Render.TemplateGroupQuestionTreeView.ServerSideReadingQuestionInGroupQuestion(SiteId,GroupQuestionId,Keyword, CurrentPageIndex,ExamTemplateId,SelectLevel).value;\r\n" +
                              "         document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                              "     }\r\n" +

                              "     function ClientRefresh(CurrentPageIndex)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                              "         document.getElementById('txtCurrentPageIndex').value=CurrentPageIndex;\r\n" +
                              "         ClientLoadQuestion(GroupQuestionId);\r\n" +
                              "     }\r\n" +
                              "</script>\r\n" +
                              "<input type=hidden id=\"txtCurrentPageIndex\" value=\"0\">\r\n" +
                              "<input type=hidden id=\"txtSelectedGroupQuestionId\" value=\"" + RootGroupQuestionId + "\">\r\n" +
                              "<h3>Ngân hàng câu hỏi</h3>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                    "<div id=\"tree\">\r\n" +
                    "<ul>\r\n";

                for (int iIndex = 0; iIndex < OGroupQuestion.Length; iIndex++)
                {
                    bool CheckFunAcces = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupQuestionPermission.StaticId, GroupQuestionPermission.funcAccess, OGroupQuestion[iIndex].GroupQuestionId, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                    if (!CheckFunAcces)
                        continue;
                    Html +=

                    " <li class=\"folder expanded\"><a href=\"" + OGroupQuestion[iIndex].GroupQuestionId + "\" target=\"_self\">" + OGroupQuestion[iIndex].GroupQuestionName + "</a>\r\n" +
                    "     <ul>\r\n";
                    GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, OGroupQuestion[iIndex].GroupQuestionId);
                    for (int iIndex1 = 0; iIndex1 < GroupQuestions.Length; iIndex1++)
                    {
                        Html +=
                                "<li class=\"folder expanded\"><a href=\"" + GroupQuestions[iIndex1].GroupQuestionId + "\" target=\"_self\">" + GroupQuestions[iIndex1].GroupQuestionName + "</a>\r\n" +
                                    BuildSubTreeView(SiteId, GroupQuestions[iIndex1].GroupQuestionId).HtmlContent +
                                "</li>\r\n";
                    }

                    Html += "</ul>\r\n" +
                            "</li>\r\n";

                }
                Html += "</ul>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId,
            string ParentGroupQuestionId
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, ParentGroupQuestionId);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupQuestions[iIndex].GroupQuestionId + "\">" + GroupQuestions[iIndex].GroupQuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupQuestions[iIndex].GroupQuestionId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFunction(
            string SiteId,
            string RootGroupQuestionId,
            string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string Html =
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientRefresh(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại danh sách</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('examtemplate.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingQuestionInGroupQuestion(
            string SiteId,
            string GroupQuestionId,
            string Keyword,
            int CurrentPageIndex,
            string ExamTemplateId,
            string SelectLevel)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                GroupQuestionCls OGroupQuestion = null;
                if (!string.IsNullOrEmpty(GroupQuestionId))
                {
                    OGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, GroupQuestionId);
                }

                if (OGroupQuestion != null)
                {
                    Html +=
                        "<h4>Đang chọn nhóm: " + OGroupQuestion.GroupQuestionName + "</h4>";
                }
                //DataTable dtQuestion = GroupQuestionBll.ReadingQuestionForGroupQuestion(SiteId, GroupQuestionId, Keyword);
                DataTable dtQuestion = GroupQuestionBll.ReadingQuestionForGroupQuestionWithExamTemp(SiteId, GroupQuestionId, Keyword, ExamTemplateId, SelectLevel);
                if (dtQuestion.Rows.Count == 0)
                {
                    Html += "<div class=CssNoData>Không có câu hỏi nào thuộc nhóm câu hỏi này</div>\r\n";
                }
                else
                {
                    ReturnPaging OReturnPaging = WebPaging.GetPaging(dtQuestion.Rows.Count, CurrentPageIndex, 25, 10, "ClientRefresh");
                    Html +=
                        OReturnPaging.PagingText +
                           "<div class=tabcontent id=divListQuestionResult>\r\n" +
                           "    <table>\r\n" +
                           "        <tr>\r\n" +
                           "            <th class=thIndex>STT</th>\r\n" +
                           "            <th>Câu hỏi</th>\r\n" +
                           "            <th>Kiểu</th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +                        
                           "        </tr>\r\n";
                    for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                    {
                        string QuestionId = (string)dtQuestion.Rows[iIndex]["QuestionId"];
                        string Type = (string)dtQuestion.Rows[iIndex]["Type"];
                        string Title = (string)dtQuestion.Rows[iIndex]["Title"];
                        Html +=
                           "        <tr>\r\n" +
                           "            <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                           "            <td><span class=CssQuestionSubject><a href=\"#\">" + Title + "</a></span></td>\r\n" +
                           "            <td>" + Type + "</td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:DeleteQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xóa câu hỏi\" title=\"Xóa câu hỏi\" class=imagebutton src=\"" + IconRoot + "/delete.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:UpdateQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Sửa câu hỏi\" title=\"Sửa\" class=imagebutton src=\""+IconRoot+"/Update.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:ViewQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xem câu hỏi\" title=\"Xem câu hỏi\" class=imagebutton src=\""+IconRoot+"/preview.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"AddDataToGroupQuestion.aspx?GroupQuestionId=" + GroupQuestionId + "&QuestionId=" + QuestionId + "\"><img alt=\"Phân vào nhóm\" title=\"Phân vào nhóm\" class=imagebutton src=\"Resources/Images/Category.png\"></a></td>\r\n" +
                           "           <td style=\"width:100px;\"><div id=\"divQuestion" + QuestionId + "\"><input type=button class=button onclick=\"javascript:ClientSelectExamQuestionTemp('" + QuestionId + "');\" value=\"Chọn câu hỏi này\"></div></td>" +
                           "        </tr>\r\n";
                    }
                    Html += "</table>\r\n" +
                            "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }





        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideGetAddGroupQuestion(
        //    string SiteId,
        //    string RootGroupQuestionId,
        //    string ParentGroupQuestionId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        OAjaxOut =
        //            GroupQuestionRenderBll.DrawAddGroupQuestionForAjax(SiteId, RootGroupQuestionId, ParentGroupQuestionId, "javascript:BackFromAddGroupQuestion();");
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetAddQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string ParentGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut.RetUrl = "AddQuestion.aspx?RootGroupQuestionId=" + RootGroupQuestionId + "&GroupQuestionId=" + ParentGroupQuestionId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetImportGroupQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string ParentGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupQuestionCls ORootGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, RootGroupQuestionId);
                GroupQuestionCls OSubGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, ParentGroupQuestionId);
                string DisplayText = ORootGroupQuestion.GroupQuestionName;
                if (OSubGroupQuestion != null)
                {
                    if (!OSubGroupQuestion.GroupQuestionId.Equals(ORootGroupQuestion.GroupQuestionId))
                    {
                        DisplayText += " / " + OSubGroupQuestion.GroupQuestionName;
                    }
                }

                string[] Types = new string[]{
                    "BestAnswer","YesNo"//,"MultiChoice"
                };
                string[] SubjectTypes = new string[]{
                    "Câu hỏi 1 phương án đúng nhất","Câu hỏi dạng đúng hoặc sai"//,"Các đáp án đúng"
                };

                string SelectTypeText = "<select class=drplist id=drpSelectType onchange=javascript:JumPageTemplateGroupQuestionTreeView(0);>";
                for (int iIndex = 0; iIndex < Types.Length; iIndex++)
                {
                    SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] + " (" + Types[iIndex] + ")</option>";
                }
                SelectTypeText += "</select>";


                string SelectLevelText =
                    "<select id=drpSelectLevelTemplateGroupQuestionTreeView class=drplist>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";


                string SelectOverwriteText =
                    "<select id=drpSelectOverwrite class=drplist>\r\n" +
                    "   <option value=\"0\">Không đè nếu tồn tại</option>\r\n" +
                    "   <option value=\"1\">Ghi đè nếu tồn tại</option>\r\n" +
                    "</select>\r\n";

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string RetHtml =
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientExecuteTemplateGroupQuestionTreeView();\"><img class=image src=\"" + IconRoot + "/Execute.png\"><div>Nhập câu hỏi</div></li>\r\n" +
                    "            <li onclick=\"javascript:ClientBackFromImport();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    " </div>\r\n" +
                    " <div class=CssFormSaveUpdate>\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <td class=Col1>Thuộc thư mục:</td>" +
                    "           <td class=Col2>" + DisplayText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Kiểu câu hỏi:</td>" +
                    "           <td class=Col2>" + SelectTypeText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Mức độ:</td>" +
                    "           <td class=Col2>" + SelectLevelText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Tham số:</td>" +
                    "           <td class=Col2>" + SelectOverwriteText + "</td>" +
                    "       </tr>" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Chọn tệp:</td>\r\n" +
                    "           <td class=Col2>\r\n" +
                    "                   <input type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\" onchange=\"fileSelected();\" /><div style=\"margin-top:10px;\"></div>\r\n" +//<input type=button class=button onclick=\"uploadFile()\" value=\"Thực hiện nhập dữ liệu từ tệp\" /><input type=button class=button onclick=\"SaveLog()\" value=\"Ghi Log import\" />
                    "                   <div id=divLinkSaveLog></div>\r\n" +
                    "                   <div id=\"fileName\"></div>\r\n" +
                    "                   <div id=\"fileSize\"></div>\r\n" +
                    "                   <div id=\"fileType\"></div>\r\n" +
                    "                   <div id=divProcessing></div>\r\n" +
                    "                   <div id=\"divImportLog\" style=\"padding:4px;border:solid 1px silver;height:200px;overflow:auto;display:none;\"></div>\r\n" +
                    "                   <input type=hidden id=txtImportFile value=\"\">\r\n" +
                    "                   <div class=CssTip style=\"color:maroon;font-size:12px;\"><i>(*) Chú ý: File phải đúng định dạng quy định theo <a href=\"ExcelTemplate/ExamStudentList.xls\">biểu mẫu</a> sau đó save as thành file text với định dạng unicode và dùng file này để import</i></div>\r\n" +
                    "           </td>\r\n" +
                    "       </tr>\r\n" +
                    "   </table>" +
                    " </div>\r\n" +
                    " <div id=divProcessingImport></div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetUpdateGroupQuestion(
            string SiteId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut =
                    GroupQuestionRenderBll.DrawUpdateGroupQuestionForAjax(SiteId, GroupQuestionId, "javascript:BackFromUpdateGroupQuestion();");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteGroupQuestion(
            string SiteId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupQuestionBll.DeleteGroupQuestion(SiteId, GroupQuestionId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetSearchForm(
            string SiteId,
            string RootGroupQuestionId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut.HtmlContent = SearchExamQuestion.Draw(SiteId, GroupQuestionId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId,
            string Keyword,
            string ExamTemplateId,
            string SelectLevel)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                new QuestionBll().DeleteQuestion(null, SiteId, QuestionId);
                OAjaxOut.HtmlContent = ServerSideReadingQuestionInGroupQuestion(SiteId, GroupQuestionId, Keyword, 0, ExamTemplateId, SelectLevel).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideUpdateQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string UpdateUrl = "UpdateQuestion.aspx?QuestionId=" + QuestionId + "&RootGroupQuestionId=" + GroupQuestionId;
                OAjaxOut.RetUrl = UpdateUrl;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideViewQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string UpdateUrl = "PreviewOneQuestion.aspx?QuestionId=" + QuestionId + "&RootGroupQuestionId=" + GroupQuestionId;
                OAjaxOut.RetUrl = UpdateUrl;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //import file
        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadMaxLine(string SiteId, string ImportFile)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(ImportFile);
                OAjaxOut.RetNumber = ds.Tables["Question"].Rows.Count;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string ServerSideGetProcessBar(string SiteId, int ProcessLine, int MaxLine)
        {
            int MaxW = 700;
            decimal W = 0;
            decimal Percent = 0;
            if (MaxLine > 0)
            {
                W = (decimal)MaxW * (decimal)ProcessLine / (decimal)MaxLine;
                Percent = ((decimal)ProcessLine / (decimal)MaxLine) * 100;
            }
            if (Percent < 1) Percent = 1;
            if (Percent > 100) Percent = 100;
            string HtmlProgressBar = "<div style=\"width:" + MaxW + "px;padding:10px; background-color:silver;border:solid 1px black;height:55px;margin-top:10px;margin-bottom:10px;\">\r\n" +
                                     "   <div style=\"padding:4px;\">Xử lý dòng " + (ProcessLine + 1) + "/" + MaxLine + "</div>\r\n" +
                                     "   <div style='border:solid 1px black;padding:4px;background-color:blue;color:white;width:" + W.ToString("###0") + "px'>" + Percent.ToString("#,##0") + "% </div>\r\n" +
                                     "</div>\r\n";

            return HtmlProgressBar;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideProcessLine(
            string SiteId,
            string GroupQuestionId,
            string QuestionType,
            string LevelImport,
            string ImportFile,
            int ProcessLine,
            int Overwrite)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            OAjaxOut.InfoMessage = "";
            QuestionCls OQuestion = new QuestionCls();
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(ImportFile);
                DataTable dtQuestion = ds.Tables["Question"];
                if (dtQuestion == null)
                {
                    return OAjaxOut;
                }
                if (dtQuestion.Rows.Count == 0)
                {
                    return OAjaxOut;
                }
                if (ProcessLine == dtQuestion.Rows.Count)
                {
                    return OAjaxOut;
                }

                DataTable dtAnswer = ds.Tables["Answer"];
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                string QuestionId = (string)dtQuestion.Rows[ProcessLine]["Id"];
                DataView dvAnswer = new DataView(dtAnswer, "QuestionId='" + QuestionId + "'", "", DataViewRowState.CurrentRows);

                OQuestion.QuestionId = QuestionId;
                OQuestion.Title = (string)dtQuestion.Rows[ProcessLine]["Subject"];
                OQuestion.Type = QuestionType;
                OQuestion.Level = int.Parse(LevelImport);
                OQuestion.Answers = new System.Collections.ObjectModel.Collection<AnswerCls> { };
                for (int iIndexAnswer = 0; iIndexAnswer < dvAnswer.Count; iIndexAnswer++)
                {
                    AnswerCls OAnswer = new AnswerCls();
                    OAnswer.AnswerId = (string)dvAnswer[iIndexAnswer]["AnswerId"];
                    OAnswer.AnswerTextFirst = (string)dvAnswer[iIndexAnswer]["AnswerText"];
                    bool Correct = bool.Parse(dvAnswer[iIndexAnswer]["IsAnswer"].ToString());
                    if (Correct)
                    {
                        OQuestion.CorrectOrder = iIndexAnswer;// ?1:0;
                    }
                    OAnswer.CorrectOrder = Correct ? 1 : 0;
                    OAnswer.IndexOrder = iIndexAnswer + 1;
                    if (string.IsNullOrEmpty(OAnswer.AnswerTextFirst) && string.IsNullOrEmpty(OAnswer.AnswerTextLast))
                    {
                        //trang khong lam gi ca
                    }
                    else
                    {
                        OQuestion.Answers.Add(OAnswer);
                    }
                }
                new QuestionBll().AddQuestion(null, SiteId, GroupQuestionId, QuestionType, int.Parse(LevelImport), OQuestion, Overwrite);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                string ErrorMsg = ex.Message.ToString();
                if (ErrorMsg.IndexOf("Violation of PRIMARY KEY") != -1)
                {
                    ErrorMsg = "Đã có câu hỏi '" + OQuestion.Title + "' trong cơ sở dữ liệu";
                }
                OAjaxOut.InfoMessage = "Lỗi xử lý dòng " + (ProcessLine + 1) + " => " + ErrorMsg;
            }
            return OAjaxOut;
        }
    }
}
